package cn.crdeng.controller.admin;

import cn.crdeng.Service.TagService;
import cn.crdeng.po.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 分页查询所有
     * @param pageable
     * @param model
     * @return
     */
    @GetMapping("/tags")
    public String getTagPage(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                             Model model){
            model.addAttribute("pageInfo",tagService.listTag(pageable));
        return "admin/tags";
    }

    /**
     * 保存
     * @param tag
     * @return
     */
    @PostMapping("/tags")
    public String saveTag(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
        //如果为空则返回
        if (result.hasErrors()){
            //获取错误信息
            String defaultMessage = result.getFieldError().getDefaultMessage();
            attributes.addFlashAttribute("message",defaultMessage);
            return "redirect:/admin/tags/input";
        }
        Tag byName = tagService.findByName(tag.getName());
        if (byName!=null){
            //如果不为空 则返回
            attributes.addFlashAttribute("message","此标签已存在");
              return "redirect:/admin/tags/input";
        }

        Tag tag1 = tagService.saveTag(tag);
        if (tag1!=null){
            //不为空，则返回保存成功
            attributes.addFlashAttribute("message","添加标签"+tag.getName()+"成功");
            return "redirect:/admin/tags";
        }else {
            //为空，则返回添加失败
            attributes.addFlashAttribute("message","添加标签"+tag.getName()+"失败");
            return "redirect:/admin/tags/input";
        }

    }

    /**
     * 修改
     * @param tag
     * @param result
     * @param id
     * @param attributes
     * @return
     */
    @PostMapping("/tags/{id}")
    public String UpdateTag(@Valid Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes attributes){
        //如果为空则返回
        if (result.hasErrors()){
            //获取错误信息
            String defaultMessage = result.getFieldError().getDefaultMessage();
            attributes.addFlashAttribute("message",defaultMessage);
            return "redirect:/admin/tags/input";
        }
        Tag byName = tagService.findByName(tag.getName());
        if (byName!=null){
            //如果不为空 则返回
            attributes.addFlashAttribute("message","此标签已存在");
            return "redirect:/admin/tags/input";
        }


        Tag tag1 = tagService.saveTag(tag);
        if (tag1!=null){
            //不为空，则返回保存成功
            attributes.addFlashAttribute("message","修改标签"+tag.getName()+"成功");
            return "redirect:/admin/tags";
        }else {
            //为空，则返回添加失败
            attributes.addFlashAttribute("message","修改标签"+tag.getName()+"失败");
            return "redirect:/admin/tags/input";
        }

    }
    //根据ID查询
     @GetMapping("/tags/{id}/input")
    public String getTagUpdate(@PathVariable Long id,Model model){
         Tag tag = tagService.getTag(id);
         model.addAttribute("tag",tag);
         return "admin/tags-input";
     }

    /**
     * 跳转添加页面
     * @param model
     * @return
     */
    @GetMapping("/tags/input")
    public  String getInputPage(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }

    /**
     *删除
     * @param id
     * @return
     */
    @GetMapping("/tags/{id}/delete")
    public String deleteTag(@PathVariable Long id,RedirectAttributes attributes){
        Tag tag = tagService.getTag(id);
        if (tag!=null) {
            tagService.deleteTag(id);
            attributes.addFlashAttribute("message","删除"+tag.getName()+"成功");
        }else {
            attributes.addFlashAttribute("message","删除失败");
        }
        return "redirect:/admin/tags";
    }
}
